package com.longyiqun.cm.controller.cases;

import com.alibaba.fastjson.JSON;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.longyiqun.cm.CaseManager;
import com.longyiqun.cm.PageUtil;
import com.longyiqun.cm.TablePage;
import com.longyiqun.cm.domain.TestCaseEntity;
import com.longyiqun.cm.service.CasesService;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.script.ScriptTemplateConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URL;

import static io.restassured.RestAssured.given;

@RestController
@RequestMapping("/api/cases")
public class CasesRestControllerImpl {

    @Autowired
    CasesService casesService;

    @Autowired
    PageUtil pageUtil;

//    @RequestMapping("/getCaseForPaging")
//    public String  getCaseForPaging(@RequestParam int pageNumber, @RequestParam int pageSize) {
//        TablePage tp= pageUtil.getDataForPaging(casesService.getModuleForPaging(pageNumber, pageSize));
//        return JSON.toJSONString (tp);
//    }

    @RequestMapping("/getCaseForPaging")
    public String  getCaseForPaging(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize, @RequestParam(required = false) String ID, @RequestParam(required = false) String module, @RequestParam(required = false) String selectType, @RequestParam(required = false) String sortName, @RequestParam(required = false) String sortOrder) {
        if (sortName == null || sortName == "") {
            TablePage tp = pageUtil.getDataForPaging(casesService.getModuleForPaging(pageNumber, pageSize, ID, module, selectType));
            return JSON.toJSONString(tp);
        } else {
            TablePage tp = pageUtil.getDataForPaging(casesService.getModuleForPaging(pageNumber, pageSize, ID, module, selectType, sortName, sortOrder));
            return JSON.toJSONString(tp);
        }
    }
    @RequestMapping("/getServerForPaging")
    public String  getServerForPaging(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize, @RequestParam(required = false) String Leader, @RequestParam(required = false) String ServerName, @RequestParam(required = false) String Platform,@RequestParam(required = false) String selectType) {
        String S =Thread.currentThread().getContextClassLoader().getResource("").getPath();
        S  = StringUtils.substringBefore(S ,"target") +"src/main/resources/static/file/";

        System.out.println(S);
            TablePage tp = pageUtil.getDataForPaging(casesService.getServerForPaging(pageNumber, pageSize, Leader, Platform,ServerName, selectType));
            return JSON.toJSONString(tp);

    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String updateCase(@RequestBody TestCaseEntity testCaseEntity) {
        int result = casesService.updateOne(testCaseEntity);
        if(result>0) {
            return JSON.toJSONString ("true");
        }else{
            return JSON.toJSONString ("false");
        }
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addCase(@RequestBody TestCaseEntity testCaseEntity) {
        int result = casesService.addOne(testCaseEntity);
        if(result>0) {
            return JSON.toJSONString ("true");
        }else{
            return JSON.toJSONString ("false");
        }
    }
    @RequestMapping(value = "/commonhttp", method = RequestMethod.POST)
    public String Commonhttp(@RequestBody TestCaseEntity testCaseEntity) {
//        JsonObject jsonObject = new JsonObject();
//        jsonObject.addProperty("Data", testCaseEntity.getBody());
        System.out.println(testCaseEntity.getBody()+testCaseEntity.getUrl());
        final Response response = given().contentType(ContentType.JSON).body(testCaseEntity.getBody()).post(testCaseEntity.getUrl());
        JsonParser jp = new JsonParser();
        JsonElement jeReturn = jp.parse(response.body().asString());
        return jeReturn.toString();

    }
    @RequestMapping(value = "/getResult",method = RequestMethod.POST)
    public void treeCardResult(@RequestBody String data){
        System.out.println("Data::::::"+data);

    }
    @RequestMapping(value = "/delete")
    public String deleteCase(@RequestParam int caseID) {
        int result = casesService.deleteOne(caseID);
        if(result>0) {
            return JSON.toJSONString ("true");
        }else{
            return JSON.toJSONString ("false");
        }
    }

    @RequestMapping(value = "/mock")
    public String mockCase(@RequestParam String caseID, @RequestParam String env) {
        System.out.println(caseID+"+++++++++++++++++++");
        System.out.println(env+"+++++++++++++++++++");
        String result = casesService.mockOne(caseID, env);
        if(result !="" && result != null) {
            return result;
        }else{
            return JSON.toJSONString ("false");
        }
    }

    @RequestMapping(value = "/runAll")
    public String runAll(@RequestParam String env) {
        int result = casesService.runAll(env);
        if(result == 0) {
            return "true";
        }else{
            return "false";
        }
    }

    @RequestMapping(value = "/runModule")
    public String runModule(@RequestParam String moduleName, @RequestParam String env) {
        int result = casesService.runModule(moduleName, env);
        if(result == 0) {
            return "true";
        }else{
            return "false";
        }
    }

    @RequestMapping(value = "/runSingle")
    public String runSingle(@RequestParam String ids, @RequestParam String env) {
        int result = casesService.runSingle(ids, env);
        if(result == 0) {
            return "true";
        }else{
            return "false";
        }
    }

    @RequestMapping(value = "/runModuleSQL")
    public String runModuleSQL(@RequestParam String moduleName, @RequestParam String env) {
        int result = casesService.runModuleSQL(moduleName, env);
        if(result == 0) {
            return "true";
        }else if (result == 2){
            return "no";
        } else {
            return "false";
        }
    }

    @RequestMapping(value = "/isFinish")
    public String isFinish(@RequestParam int jobID) {
        String result = casesService.isFinish(jobID);
        return result;
    }

    @RequestMapping(value = "/getCaseRemark")
    public String getCaseRemark(@RequestParam int caseID) {
        String result = casesService.getCaseRemark(caseID);
        return result;
    }

}
